import Public from '../api/public'
export default {
  data() {
    return {
      sendDisabled: false,
      sendText: '发送验证码',
      countdown: 59,
      timer: null
    }
  },
  methods: {
    // 发送
    send(loginName) {
      Public.User.sendCode({
        loginName
      }).then(res => {
        if (res.data.status === 0) {
          this.setCountdown()
        } else {
          this.$toast.fail(res.data.message)
        }
      })
    },
    // 倒计时
    setCountdown() {
      this.sendDisabled = true
      this.sendText = `重新发送（${this.countdown}s）`
      this.timer = setInterval(() => {
        this.countdown--
        this.sendText = `重新发送（${this.countdown}s）`
        if (this.countdown === 0) {
          if (this.timer) {
            clearInterval(this.timer)
            this.timer = null
          }
          this.countdown = 59
          this.sendDisabled = false
          this.sendText = '重新发送'
        }
      }, 1000)
    }
  }
}
